﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Disappearwind.PortalSolution.PortalWeb.Models;

namespace Disappearwind.PortalSolution.PortalWeb.Business
{
    /// <summary>
    /// Business for PortalMenu
    /// </summary>
    public class PortalMenuBusiness : BaseBusiness
    {
        /// <summary>
        /// Get PortalMenu by keywor 
        /// </summary>
        /// <param name="keyWord">keyword</param>
        /// <returns></returns>
        public PortalMenu GetPortalMenu(string keyWord)
        {
            var c = from p in DBContext.PortalMenu
                    where p.Keyword == keyWord
                        & p.State == true
                    select p;
            if (c != null && c.Count() > 0)
            {
                return c.FirstOrDefault();
            }
            else
            {
                return new PortalMenu();
            }
        }
        /// <summary>
        /// Get all children of current menu
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public List<PortalMenu> GetChildrenPortalMenu(PortalMenu menu)
        {
            var c = from p in DBContext.PortalMenu
                    where p.ParentMenu == menu.Id
                        & p.State == true
                    select p;
            if (c != null && c.Count() > 0)
            {
                return c.ToList();
            }
            else
            {
                return new List<PortalMenu>();
            }
        }
        /// <summary>
        /// Get PortalMenu by portalId, will return all PortalMenu in the portal
        /// </summary>
        /// <param name="portalId"></param>
        /// <returns></returns>
        public List<PortalMenu> GetPortalMenuList(int portalId)
        {
            var c = from p in DBContext.PortalMenu
                    where p.PortalId == portalId
                    select p;
            if (c == null || c.Count() == 0)
            {
                return new List<PortalMenu>();
            }
            else
            {
                return c.ToList();
            }
        }
        /// <summary>
        /// Get PortalMenu by menuId,if not exist return a empty PortalMenu object
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public PortalMenu GetPortalMenu(int menuId)
        {
            var c = from p in DBContext.PortalMenu
                    where p.Id == menuId
                    select p;
            if (c != null && c.Count() > 0)
            {
                return c.FirstOrDefault();
            }
            else
            {
                return new PortalMenu();
            }
        }
        /// <summary>
        /// Add a new PortalMenu to database
        /// </summary>
        /// <param name="portalMenu"></param>
        /// <returns></returns>
        public bool AddPortalMenu(PortalMenu portalMenu)
        {
            if (ValidatePortalMenu(portalMenu))
            {
                DBContext.AddToPortalMenu(portalMenu);
                DBContext.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Update a PotalMenu
        /// </summary>
        /// <param name="portalMenu"></param>
        /// <returns></returns>
        public bool UpdatePortalMenu(PortalMenu portalMenu)
        {
            if (ValidatePortalMenu(portalMenu))
            {
                PortalMenu originalPortalMenu = GetPortalMenu(portalMenu.Id);
                DBContext.ApplyPropertyChanges(originalPortalMenu.EntityKey.EntitySetName, portalMenu);
                DBContext.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Delete a PortalMenu
        /// </summary>
        /// <param name="menuId"></param>
        public void DeletePortalMenu(int menuId)
        {
            PortalMenu originalPortalMenu = GetPortalMenu(menuId);
            DBContext.DeleteObject(originalPortalMenu);
            DBContext.SaveChanges();
        }
        /// <summary>
        /// Validate the PortalMenu
        /// </summary>
        /// <param name="portalMenu"></param>
        /// <returns></returns>
        public bool ValidatePortalMenu(PortalMenu portalMenu)
        {
            CleanErrorDic();
            if (string.IsNullOrEmpty(portalMenu.Name))
            {
                ModelStateDic.AddModelError("Name", "Name should not be empty.");
            }
            if (string.IsNullOrEmpty(portalMenu.Keyword))
            {
                ModelStateDic.AddModelError("Keyword", "Keyword should not be empty.");
            }
            List<PortalMenu> allPortalMenu = DBContext.PortalMenu.ToList();
            //validate update portalinfo
            if (portalMenu.Id != 0)
            {
                var c = allPortalMenu.Where(p => p.Id != portalMenu.Id && p.Keyword == portalMenu.Keyword);
                if (c != null && c.Count() > 0)
                {
                    ModelStateDic.AddModelError("Keyword", "The Keyword has already exist.");
                }
            }
            else
            {
                var c = allPortalMenu.Where(p => p.Keyword == portalMenu.Keyword);
                if (c != null && c.Count() > 0)
                {
                    ModelStateDic.AddModelError("Keyword", "The Keyword has already exist.");
                }

            }
            return ModelStateDic.IsValid;
        }
    }
}
